package com.gongyibao.gbase.mq.thread;

/**
 * @Author: 张启峰
 * @Date: 2023/4/17 14:47
 * @Description: gyb-
 */

import com.cvicse.inforsuitemq.InforsuiteMQConnectionFactory;

import javax.jms.*;
import java.text.SimpleDateFormat;
import java.util.Date;

class MessagesThroughputSender implements Runnable {
    private Thread t;
    private String user = "admin";
    private String password = "admin";
    private String url = "failover:(tcp://192.168.6.55:8688)";
    private Destination destination;
    private String subject;
    private boolean persistent;
    private int messageSize;
    private int messagecount;
    private Session session;

    MessagesThroughputSender(String sub, boolean per, int msz, int amount, Session sess) {
        subject = sub;
        persistent = per;
        messageSize = 1024 * msz;
        messagecount = amount;
        session = sess;
    }

    MessagesThroughputSender() {
    }

    public void start() {
        t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            destination = session.createQueue(subject);
            MessageProducer producer = session.createProducer(destination);
            if (persistent) {
                producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            } else {
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            }
            // Start sending messages
            sendLoop(session, producer);// 调用发送消息函数
            producer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendLoop(Session session, MessageProducer producer) throws Exception {
        long start, end, start1, end1;
        String createMessageText = createMessageText();
        TextMessage message = session.createTextMessage(createMessageText);
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        start = System.currentTimeMillis();
        for (int i = 0; i < messagecount; i++) {
            producer.send(message);
        }
        end = System.currentTimeMillis();
        System.out.println("start time:" + start + "end time:" + end + "发送消息耗时Run Time:" + (end - start) + "(ms)"
                + " 发送消息结束" + df1.format(new Date()));
    }

    private String createMessageText() {
        StringBuffer buffer = new StringBuffer(messageSize);
        for (int i = buffer.length(); i < messageSize * 128; i = i + 1024) {
            buffer.append("qqqqqqqq");
        }
        return buffer.toString();
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setPersistent(boolean persistent) {
        this.persistent = persistent;
    }

    public void setMessageSize(int messageSize) {
        this.messageSize = messageSize;
    }

    public Session getSession() {
        Connection connection = null;
        Session session = null;
        // Create the connection
        InforsuiteMQConnectionFactory connectionFactory = new InforsuiteMQConnectionFactory(user, password, url);
        connectionFactory.setUseCompression(true);
        connectionFactory.setProducerWindowSize(1024000);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return session;
    }
}
